package com.dm.dao.impl;

import com.dm.dao.BaseDao;
import com.dm.dao.FolderDao;
import com.dm.dto.FolderAttributeDto;
import com.dm.dto.FoldersDto;
import com.dm.dto.input.FolderDto;
import com.dm.entity.Folder;
import org.hibernate.Query;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

/**
 * @author hqw521@qq.com
 * @date 2018/11/16 10:40
 */
@Repository
public class FolderDaoImpl implements FolderDao {
    @Autowired
    private BaseDao baseDao;

    @Override
    public Integer insertFolder(FolderDto folderDto) {
        String sql = "insert into dm_folder (folder_name, folder_keyWord, user_id) values (?, ?, ?)";
        Query query = baseDao.getSession().createSQLQuery(sql);
        query.setString(0, folderDto.getFolderName());
        query.setString(1, folderDto.getFolderKeyWord());
        query.setInteger(2, folderDto.getUserId());
        return query.executeUpdate();
    }

    @Override
    public Integer insertParentFolder(FolderDto folderDto) {
        String sql = "insert into dm_folder (folder_name, folder_keyWord, user_id, parent_folder) values (?, ?, ?, ?)";
        Query query = baseDao.getSession().createSQLQuery(sql);
        query.setString(0, folderDto.getFolderName());
        query.setString(1, folderDto.getFolderKeyWord());
        query.setInteger(2, folderDto.getUserId());
        query.setInteger(3, folderDto.getParentFolder());
        return query.executeUpdate();
    }

    @Override
    public List<Folder> findFolders() {
        String hql = "select f from Folder f where f.parentFolder is null";
        Query query = baseDao.getSession().createQuery(hql);
        List<Folder> list = query.list();
        return list;
    }

    @Override
    public List<FoldersDto> findFoldersByParentFolder(Integer parentFolder) {
        String sql = "select df.folder_id folderId, df.folder_name folderName from dm_folder df where df.parent_folder = " + parentFolder;
        Query query = baseDao.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(FoldersDto.class));
        List<FoldersDto> foldersDtos = query.list();
        return foldersDtos;
    }

    @Override
    public Integer countPeerFolderByName(String folderName, Integer folderId) {
        String sql = "select count(*) from dm_folder df where df.parent_folder in (select parent_folder from dm_folder a where a.folder_id = " + folderId + ") and df.folder_name = '" + folderName + "'";
        Query query = baseDao.getSession().createSQLQuery(sql);
        return Integer.valueOf(query.uniqueResult().toString());
    }

    @Override
    public Integer folderRename(String folderName, Integer folderId) {
        String hql = "update Folder f set f.folderName = '" + folderName + "' where f.folderId = " + folderId;
        Query query = this.baseDao.getSession().createQuery(hql);
        return query.executeUpdate();
    }

    @Override
    public FolderAttributeDto findFolderAttribute(Integer folderId) {
        String sql = "SELECT\n" +
                "\tcount(df2.file_id) fileSize,\n" +
                "\ta.folderSize,\n" +
                "\tdf3.create_time createTime,\n" +
                "\tsum(df2.file_size) size\n" +
                "FROM\n" +
                "\tdm_file df2,\n" +
                "\tdm_folder df3,\n" +
                "\t(\n" +
                "\t\tSELECT\n" +
                "\t\t\tcount(df.folder_id) folderSize\n" +
                "\t\tFROM\n" +
                "\t\t\tdm_folder df\n" +
                "\t\tWHERE\n" +
                "\t\t\tdf.parent_folder = " + folderId + "\n" +
                "\t) a\n" +
                "WHERE\n" +
                "\tdf2.folder_id = " + folderId + "\n" +
                "AND df3.folder_id = " + folderId;
        Query query = baseDao.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(FolderAttributeDto.class));

        return (FolderAttributeDto) query.uniqueResult();
    }
}
